﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestComData4ZigBee
{
    public class ZigbeeEntity
    {
        public double temperature;
        public double hum;
        public double waterTemp;
        public double waterLevel;
        public double alcohol;
        public double light;
        public double co;
        public double firegas;
        public double fire;
        public double value1;
        public double value2;
        public double value3;
        public double value4;
        public int persion;
        public int zigbee_panid1;
        public int zigbee_panid2;
        public int zigbee_chancel;
    }
    public class Tool
    {
        private const int index4HEAD = 0;
        private const int index4LEN = 1;
        private const int index4CMD0 = 2;
        private const int index4CMD1 = 3;

        private const int index4ADRL = 4;
        private const int index4ADRH = 5;
        private const int index4DTYPEL = 6;
        private const int index4DTYPEH = 7;

        private const int index4DLEN = 8;
        private const int index4REV = 9;
        private const int index4INTEMP = 10;
        private const int index4INVOL = 11;
        private const int index4PARADRL = 12;
        private const int index4PARADRH = 13;
        private const int index4LGTYPE = 14;
        private const int index4SORL = 15;
        private const int index4SORH = 16;
        private const int index4STYPE = 17;
        private const int index4D1L = 18;
        private const int index4D1H = 19;
        private const int index4D2L = 20;
        private const int index4D2H = 21;
        private const int index4D3L = 22;
        private const int index4D3H = 23;


        private const byte HEAD = 0xFE;
        //LEN
        private const byte CMD0 = 0x46;
        private const byte CMD1 = 0x87;
        //ADRL
        //ADRH
        private const byte DTYPEL = 0x02;
        private const byte DTYPEH = 0x00;
        //DLEN
        private const byte REV = 0x00;
        // INTEMP
        // INVOL
        // PARADRL
        // PARADRH

        // LGTYPE
        // SORL
        // SORH
        // STYPE
        // D1L
        // D1H
        // D2L
        // D2H
        // D3L
        // D3H

        public static float convert(byte data1, byte data2)
        {
            String tempString = String.Format("{0:X}", data2);//Integer.toHexString(data2);
            if (tempString.Length == 1)
                tempString = "0" + tempString;

            String strLight = String.Format("{0:X}", data1) + tempString;
            short lightValue = 0;//Integer.parseInt(strLight, 16);
            Int16.TryParse(strLight, out lightValue);
            return lightValue;
        }

        public static int convertEx(byte dataH, byte dataL)
        {
            int nRet = 0;

            nRet = (nRet | dataH);
            nRet = nRet << 8;
            nRet = (nRet | dataL);

            return nRet;
        }
        private static int findStartPos4Data(byte[] arraySrcData,
                                      int nSrcLength,
                                      int curStartPos4Data)
        {
            if (arraySrcData == null)
                return -1;
            if (arraySrcData.Length < nSrcLength)
                return -1;
            if (arraySrcData.Length < curStartPos4Data)
                return -1;


            for (int index = curStartPos4Data; index < arraySrcData.Length; index++)
            {
                if (arraySrcData[index] != HEAD)
                    continue;

                int dataLength = nSrcLength - index;//15-0
                if (dataLength < 15)// must >=15
                    continue;
                

                int nValue4LEN = arraySrcData[index + index4LEN];               
                int nValue4DLEN = arraySrcData[index + index4DLEN];
                if (nValue4LEN != (6 + nValue4DLEN))
                    continue;
                
                if (arraySrcData[index + index4CMD0] != CMD0 ||
                   arraySrcData[index + index4CMD1] != CMD1 ||
                   arraySrcData[index + index4DTYPEL] != DTYPEL ||
                   arraySrcData[index + index4DTYPEH] != DTYPEH ||
                   arraySrcData[index + index4REV] != REV)
                    continue;

                return index;
            }

            return -1;
        }
        public static ZigbeeEntity HandlerData(byte[] arraySrcData, int nSrcLength)
        {
            if (arraySrcData == null)
                return null;
            ZigbeeEntity zigBeeEntity = new ZigbeeEntity();
            int startPos4Data = 0;
            int difEndToStartPos = nSrcLength - startPos4Data;
            while (difEndToStartPos > 0)
            {
                int mark_head = findStartPos4Data(arraySrcData, nSrcLength, startPos4Data);

                if (mark_head < 0)
                    break;

                startPos4Data = mark_head + arraySrcData[mark_head+1]+4+1;
                difEndToStartPos = nSrcLength - startPos4Data;

                if (arraySrcData[(index4LGTYPE + mark_head)] != 0x01)
                {
                    int Type = arraySrcData[(index4STYPE + mark_head)];
                    switch (Type)
                    {
                        case 1:
                            byte[] temperatureTemp = new byte[2];
                            temperatureTemp[0] = arraySrcData[(index4D1L + mark_head)];
                            temperatureTemp[1] = arraySrcData[(index4D1H + mark_head)];
                            float temperatureFloat = convertEx(temperatureTemp[1], temperatureTemp[0]) / 10.0F;


                            byte[] humTemp = new byte[2];
                            humTemp[0] = arraySrcData[(index4D2L + mark_head)];
                            humTemp[1] = arraySrcData[(index4D2H + mark_head)];
                            float humFloat = convertEx(humTemp[1], humTemp[0]) / 10.0F;

                            zigBeeEntity.temperature = temperatureFloat;
                            zigBeeEntity.hum = humFloat;
                            break;
                        case 17:
                            int personValue = arraySrcData[(18 + mark_head)];
                            zigBeeEntity.persion = personValue;
                            break;
                        case 19:
                            byte[] alcoholTemp = new byte[2];
                            alcoholTemp[0] = arraySrcData[(18 + mark_head)];
                            alcoholTemp[1] = arraySrcData[(19 + mark_head)];
                            float strAolcol = convertEx(
                                                  alcoholTemp[1],
                                                  alcoholTemp[0]) / 100.0F;
                            zigBeeEntity.alcohol = strAolcol;
                            break;
                        case 33:
                            byte[] lightTemp = new byte[2];
                            lightTemp[0] = arraySrcData[(18 + mark_head)];
                            lightTemp[1] = arraySrcData[(19 + mark_head)];
                            float lightFloat = convertEx(
                                                    lightTemp[1],
                                                    lightTemp[0]) / 100.0F;
                            zigBeeEntity.light = lightFloat;
                            break;
                        case 34:
                            byte[] coTemp = new byte[2];
                            coTemp[0] = arraySrcData[(18 + mark_head)];
                            coTemp[1] = arraySrcData[(19 + mark_head)];
                            float coFloat = convertEx(coTemp[1],
                                                      coTemp[0]) / 100.0F;
                            zigBeeEntity.co = coFloat;
                            break;
                        case 35:
                            byte[] firegasTemp = new byte[2];
                            firegasTemp[0] = arraySrcData[(18 + mark_head)];
                            firegasTemp[1] = arraySrcData[(19 + mark_head)];

                            float strGas = convertEx(firegasTemp[1],
                                                     firegasTemp[0]) / 100.0F;
                            zigBeeEntity.firegas = strGas;
                            break;
                        case 36:
                            byte[] fireTemp = new byte[2];
                            fireTemp[0] = arraySrcData[(18 + mark_head)];
                            fireTemp[1] = arraySrcData[(19 + mark_head)];
                            float strfire = convertEx(fireTemp[1],
                                                      fireTemp[0]) / 100.0F;
                            zigBeeEntity.fire = strfire;
                            break;
                        case 48:
                            byte[] chars1 = new byte[2];
                            byte[] chars2 = new byte[2];
                            byte[] chars3 = new byte[2];
                            byte[] chars4 = new byte[2];
                            chars1[0] = arraySrcData[(19 + mark_head)];
                            chars1[1] = arraySrcData[(18 + mark_head)];

                            chars2[0] = arraySrcData[(21 + mark_head)];
                            chars2[1] = arraySrcData[(20 + mark_head)];

                            chars3[0] = arraySrcData[(23 + mark_head)];
                            chars3[1] = arraySrcData[(22 + mark_head)];

                            chars4[0] = arraySrcData[(25 + mark_head)];
                            chars4[1] = arraySrcData[(24 + mark_head)];

                            //String sss = charArrayToHexString(chars1);
                            float v = convertEx(chars1[1], chars1[0]);//Integer.parseInt(sss, 16);
                            float vv = v * 3300.0F / 1023.0F / 150.0F;
                            if (vv <= 4.0F)
                            {
                                vv = 4.0F;
                            }
                            zigBeeEntity.value1 = ((vv - 4.0F) / 16.0F * 70.0F - 10.0F);

                            //  String sss2 = charArrayToHexString(chars2);
                            float v2 = convertEx(chars2[1], chars2[0]);//Integer.parseInt(sss2, 16);
                            float vv2 = v2 * 3300.0F / 1023.0F / 150.0F;
                            if (vv2 < 4.0F)
                            {
                                vv2 = 4.0F;
                            }
                            zigBeeEntity.value2 = ((vv2 - 4.0F) / 16.0F * 50.0F);

                            //String sss3 = charArrayToHexString(chars3);
                            float v3 = convertEx(chars3[1], chars3[0]);//Integer.parseInt(sss3, 16);
                            float vv3 = v3 * 3300.0F / 1023.0F / 150.0F;
                            if (vv3 <= 4.0F)
                            {
                                vv3 = 4.0F;
                            }
                            zigBeeEntity.value3 = ((vv3 - 4.0F) / 16.0F * 200.0F - 50.0F);

                            // String sss4 = charArrayToHexString(chars4);
                            float v4 = convertEx(chars4[1], chars4[0]);//Integer.parseInt(sss4, 16);
                            float vv4 = v4 * 3300.0F / 1023.0F / 150.0F;
                            if (vv4 < 4.0F)
                            {
                                vv4 = 4.0F;
                            }
                            zigBeeEntity.value4 = ((vv4 - 4.0F) / 16.0F * 1.0F);
                            break;
                    }// switch (Type)
                }//if (this.data[(index4LGTYPE + mark_head)] != 0x01)
            }//while

            return zigBeeEntity;
        }//HandlerData
    }
}

